global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/figure_5a_hartz_eventstudy_a.log, replace name(fig)

capture noi {


    * Hartz reform figure - panel A Plots yearly coefficients for regressions
    clear
    set scheme plotplain
        
    * set parameters
    global target "DE"
    local y_target "2003"
    local rangecal "1995(1)2012"
    local ycondition "if year>=1995 & year <= 2012"
    local pauto "pauto95"

    * prepare data: load baseline reg data, drop domestic firms and maxshare in target country firms, keep relevant variables
    use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
    keep if country_shr_1995 != "${target}"
    keep if missing_weights_1995 == 0 & maxweight_1995 < 1
    mmerge BvD using ${dataset_dir}/weights/bvdid_pat_weights_EPtr_from1970_1995_orbis2017_tfacit1.dta, ukeep(share2_all_1995_${target}) unmatched(master)
    ren share2_all_1995_${target} weight_${target}
    ren country_shr_1995 country
    gen target_country = "${target}"
    keep BvDIDnumber lse_id industry year ${depvar}_${ttt} k*${depvar}_${ttt}* country target_country weight_${target}

    * define stocks
    gen stockown = k${depvar}_${ttt}
    gen stockownzero = k${depvar}_${ttt}0
    gen stockother = k`pauto'_${ttt}
    gen stockotherzero = k`pauto'_${ttt}0

    * build country-year FE and industry-year-FE
    egen country_year = group(country year)
    egen industry_year = group(industry year)

    * build target exposure - year FE dummies, set to zero in relative year
    forvalues i = `rangecal' {
        gen YY_`i' = 1 if year == `i' 
        replace YY_`i' = 0 if YY_`i' ==. 
        gen beta_`i' = weight_${target} * YY_`i'
        drop YY_`i'
    }
    replace beta_`y_target'= 0

    * run event study reg
    xtset lse_id year
    local ycondition "if year>=1995 & year <= 2012"
    ppmlhdfe F2.${depvar}_${ttt} beta_* stockown stockownzero stockother stockotherzero `ycondition', absorb(lse_id country_year industry_year) vce(cluster lse_id)

    * plot betas
    mat b = e(b)
    mat V = e(V)
    mat b = b[1,"beta_1995".."beta_2012"]
    mat V = V["beta_1995".."beta_2012","beta_1995".."beta_2012"]
    mat conf = J(2,`=colsof(b)',.)

    forvalues i = 1(1)`=colsof(b)' {
        mat conf[1,`i'] = b[1,`i'] + invnormal(0.975)*sqrt(V[`i',`i'])
        mat conf[2,`i'] = b[1,`i'] - invnormal(0.975)*sqrt(V[`i',`i'])
    }

    mata : st_matrix("conf_max",rowmax(st_matrix("conf")[1,.]))
    mata : st_matrix("conf_min",rowmin(st_matrix("conf")[2,.]))

    qui include ${code_dir}/config/figuretools.do

    colorpalette ptol, n(1) nograph
    coefplot, omitted keep(beta_1995 beta_1996 beta_1997 beta_1998 beta_1999 beta_2000 beta_2001 beta_2002 beta_2003 ///
        beta_2004 beta_2005 beta_2006 beta_2007 beta_2008 beta_2009 beta_2010 beta_2011 beta_2012) vertical xlabel(1 "1997" 3 "1999" 5 "2001" 7 "2003" 9 "2005" 11 "2007" 13 "2009" 15 "2011" 17 "2013") xscale(r(1 17)) ///
        msymbol("solid") recast(connected) ciopts(lwidth(0.2) recast(rarea) color("68 119 170%20") lwidth(none)) ///
        msymb(circle) mcol("68 119 170") lcolor("68 119 170") ///
        yline(0,lcolor(gs8)) ytitle("Coefficient") xtitle("Year") ylabel(-4 "-4" -2 "-2" 0 "0" 2 "2" 4 "4") yscale(r(-5.75 4)) ///
        text(-4  8 "{it:Hartz}" "{it:reforms}", just(center)) ///
        addplot(pcarrowi -4.8 7 -4.8 9, recast(pcbarrow) lwidth(thin) mcolor(gs4) barbsize(0) lcolor(gs4), below)
    graph export ${fig_dir}/main/Figure_5a_hartz_eventstudy_a.pdf, as(pdf) replace

    *eps version
    coefplot, omitted keep(beta_1995 beta_1996 beta_1997 beta_1998 beta_1999 beta_2000 beta_2001 beta_2002 beta_2003 ///
        beta_2004 beta_2005 beta_2006 beta_2007 beta_2008 beta_2009 beta_2010 beta_2011 beta_2012) vertical xlabel(1 "1997" 3 "1999" 5 "2001" 7 "2003" 9 "2005" 11 "2007" 13 "2009" 15 "2011" 17 "2013") xscale(r(1 17)) ///
        msymbol("solid") recast(connected) ciopts(lwidth(0.2) recast(rarea) color("68 119 170%20") lwidth(none)) ///
        msymb(circle) mcol("68 119 170") lcolor("68 119 170") ///
        yline(0,lcolor(gs8)) ytitle("Coefficient") xtitle("Year") ylabel(-4 "-4" -2 "-2" 0 "0" 2 "2" 4 "4") yscale(r(-5.75 4)) ///
        text(-4  8 "{it:Hartz}" "{it:reforms}", just(center)) ///
        addplot(pcarrowi -4.8 7 -4.8 9, recast(pcbarrow) lwidth(thin) mcolor(gs4) barbsize(0) lcolor(gs4), below)
    graph export ${fig_dir}/main/fig5a.eps, as(eps) replace



    * report numbers that we state in paper
    log using ${numb_dir}/Figure_5a_hartz_eventstudy_a_numbers.log, replace name(numbers)
    mat list b  
    preserve
    gen in_sample = 1 if e(sample)
    keep if in_sample == 1
    keep BvD in_sample
    duplicates drop BvD, force
    tab in_sample
    mmerge BvD using ${dataset_dir}/weights/bvdid_pat_weights_EPtr_from1970_1995_orbis2017_tfacit1.dta, unmatched(master) ukeep(share2_all_1995_DE)
    su share2_all_1995_DE
    restore
    log close numbers
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig